package data.join.com.database.bean;

import java.util.List;

import data.join.com.database.GreenDao;

/**
 * Created by Administrator on 2017/9/28.
 */

public class UpdatePref {

    public static int UPDATE_CREATE = 0;
    public static int UPDATE_DONE = 1;
    public static int UPDATE_SUCCESS = 2;

    public static int UPDATE_ERROR = 1001;
    public static int UPDATE_FAIL = 1002;

    private static UpdatePref updatePref;

    private UpdateBeanDao updateBeanDao;

    private UpdatePref(){
        updateBeanDao = GreenDao.getInstance().sDaoSession.getUpdateBeanDao();

        FileNameSyncDao.createTable(updateBeanDao.getDatabase(),true);
    }

    public static UpdatePref getInstance(){
        if(updatePref == null){
            updatePref = new UpdatePref();
        }
        return updatePref;
    }

    public void insertUpdate(UpdateBean updateBean){
        updateBeanDao.insert(updateBean);
    }

    public UpdateBean queryLastItem(){
        UpdateBean updateBean = null;
        List<UpdateBean> list = updateBeanDao.queryBuilder().orderDesc(UpdateBeanDao.Properties.Id).limit(1).list();

        if (list != null && list.size() > 0){
            updateBean = list.get(0);
        }

        return updateBean;
    }

    public boolean existCommandById(String commandId){
        return updateBeanDao.queryBuilder().where(UpdateBeanDao.Properties.CommandId.eq(commandId)).count()>0;
    }

    public void deleteItem(UpdateBean updateBean){
        updateBeanDao.deleteByKey(updateBean.getId());
    }

    public void update(UpdateBean updateBean){
        updateBeanDao.update(updateBean);
    }
}
